package org.bagou.xiangs.framework_mvvm.newer

import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_new_mvvm.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import org.bagou.xiangs.framework_mvvm.R


@Route(path = "/new/activity/NewMvvmActivity")
class NewMvvmActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new_mvvm)

        MainScope().launch(Dispatchers.IO) {
        }
        getUserInfoPromote()

    }


    private fun onUpdateUI(wechatInfo: WechatInfo) {
        val userAvatar: ImageView = user_avatar
        Glide.with(this).load(wechatInfo.wechatAvatar).into(userAvatar)
        user_name.text = wechatInfo.wechatName
        user_id.text = wechatInfo.wechatID

        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = RecyclerView.VERTICAL
        recycle_list.layoutManager = layoutManager
        val adapter = MvvmAdapter()
        recycle_list.adapter = adapter
        adapter.addData(wechatInfo.list)
    }

    private fun getUserInfoPromote() {
        val viewModelProvider = ViewModelProvider(this, ViewModelProvider.NewInstanceFactory())
        val viewModel = viewModelProvider.get(ViewModelMvvm::class.java)
        viewModel.getUserInfoPromote(this@NewMvvmActivity).observe(this@NewMvvmActivity,
            Observer<WechatInfo> { t ->
                t?.let { wechatInfo ->
                    Log.d("NewMvvmActivity", wechatInfo.toString())
                    runOnUiThread {
                        onUpdateUI(wechatInfo)
                    }
                }
            })
    }


}
